package com.ming.yourdiary.view;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.ming.yourdiary.R;
import com.ming.yourdiary.entity.DiaryNote;

public class DiaryNoteDetailDialog extends BaseDialog implements View.OnClickListener {

  private static final String TAG = "DiaryNoteDetailDialog";

  //View
  private TextView mDateDayText;
  private TextView mDateWeekdayText;
  private TextView mNoteTitleText;
  private LinearLayout mNoteContentContainer;
  private TextView mNoteContentText;
  private ImageView mImageView;

  //data
  private DiaryNote mDiaryNote;

  public void setData(DiaryNote diaryNote) {
    mDiaryNote = diaryNote;
  }

  public DiaryNoteDetailDialog(@NonNull Context context) {
    super(context);
  }

  public DiaryNoteDetailDialog(@NonNull Context context, int themeResId) {
    super(context, themeResId);
  }

  protected DiaryNoteDetailDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
    super(context, cancelable, cancelListener);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initView();
    initData();
  }

  private void initView() {
    LinearLayout rootView = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.diary_note_detail_dialog_layout, null);
    setContentView(rootView);
    mDateDayText = (TextView) rootView.findViewById(R.id.detail_dialog_date_day);
    mDateWeekdayText = (TextView) rootView.findViewById(R.id.detail_dialog_date_weekday);
    mNoteTitleText = (TextView) rootView.findViewById(R.id.detail_dialog_note_title);
    mNoteContentContainer = (LinearLayout) rootView.findViewById(R.id.detail_dialog_note_content_container);
    mNoteContentText = (TextView) rootView.findViewById(R.id.detail_dialog_note_content_tv);
    mImageView = (ImageView)rootView.findViewById(R.id.detail_dialog_note_content_img);

    rootView.setOnClickListener(this);
  }

  private void initData() {
    if (mDiaryNote != null) {
      mDateDayText.setText(String.valueOf(mDiaryNote.getMonthDay()));
      mDateWeekdayText.setText(mDiaryNote.getWeekDay());
      mNoteTitleText.setText(mDiaryNote.getTitle());
      mNoteContentText.setText(mDiaryNote.content);
      Glide.with(getContext()).load(R.drawable.test)
        .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
        .into(mImageView);
    } else {
      dismiss();
    }
  }

  public static DiaryNoteDetailDialog newInstance(Context context, DiaryNote content) {
    if (content != null) {
      DiaryNoteDetailDialog dialog = new DiaryNoteDetailDialog(context, R.style.default_fullscreen_dialog);
      dialog.setData(content);
      dialog.show();
      return dialog;
    }
    return null;
  }


  @Override
  public void onClick(View v) {
    if (v.getId() == R.id.detail_dialog_date_day) {
      dismiss();
    }
  }
}
